package cz.muni;

import com.fasterxml.jackson.databind.ObjectMapper;
import cz.muni.restinterface.CourierDTO;
import cz.muni.service.CourierServiceLocal;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 *
 * @author Thomas, Ondrej
 */
@WebServlet(urlPatterns = "/Rest/Courier/*")
public class CourierServlet extends HttpServlet {

    @EJB
    private CourierServiceLocal courierService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        Map<String, String[]> params = request.getParameterMap();
        ObjectMapper mapper = new ObjectMapper();

        for (Iterator<Entry<String, String[]>> it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = it.next();
            
            

            String key = (String) entry.getKey();
            System.out.println(key);
            if (key.contains("findbyid")) {
                Long id = Long.valueOf(request.getParameter("findbyid"));
                CourierDTO courier = courierService.find(id);
                if (courier != null) {
                    response.setContentType("application/json");
                    mapper.writeValue(response.getOutputStream(), courier);
                } else {
                    response.setContentType("application/json");
                    mapper.writeValue(response.getOutputStream(), new String("Wrong courier id"));
                }

            } else if (key.contains("findall")) {
                List<CourierDTO> list = courierService.findAll();
                response.setContentType("application/json");
                mapper.writeValue(response.getOutputStream(), list);

            }
        }
    }

    @Override
    protected synchronized void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        Map<String, String[]> params = request.getParameterMap();

        for (Entry<String, String[]> e : params.entrySet()) {
            if (e.getKey().contains("create")) {
                CourierDTO c = new CourierDTO();
                c.setFirstName(params.get("firstName")[0]);
                c.setLastName(params.get("lastName")[0]);
                c.setUserName(params.get("username")[0]);
                courierService.create(c);
            } else if (e.getKey().contains("update")) {
                CourierDTO c = courierService.find(Long.valueOf(params.get("id")[0]));
                if (c != null) {
                    c.setFirstName(params.get("firstName")[0]);
                    c.setLastName(params.get("lastName")[0]);
                    c.setUserName(params.get("username")[0]);
                    courierService.update(c);
                }
            }
        }
    }

    @Override
    protected synchronized void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        CourierDTO courier = courierService.find(Long.valueOf(request.getParameter("delete")));
        if (courier != null) {
            courierService.delete(courier);
        }
    }

    @Override
    protected synchronized void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        Map<String, String[]> params = request.getParameterMap();
        ObjectMapper mapper = new ObjectMapper();

        for (Iterator<Entry<String, String[]>> it = params.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = it.next();

            String key = (String) entry.getKey();
            if (key.contains("update")) {

                mapper = new ObjectMapper();
                CourierDTO courier = mapper.readValue(request.getInputStream(), CourierDTO.class);
                courierService.update(courier);
            }
        }
    }
}